package com.hlf.modules.api.controller;


import com.hlf.common.annotation.SysLog;
import com.hlf.common.constants.Constants;
import com.hlf.common.utils.IPUtils;
import com.hlf.common.utils.ReturnResult;
import com.hlf.common.validator.Assert;
import com.hlf.modules.api.annotation.AuthIgnore;
import com.hlf.modules.api.annotation.LoginUser;
import com.hlf.modules.merchant.merchant.entity.Merchant;
import com.hlf.modules.merchant.merchant.service.MerchantService;
import com.hlf.modules.sys.entity.SysUserEntity;
import com.hlf.modules.sys.service.SysUserService;
import com.hlf.modules.sys.service.SysUserTokenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * API登录授权
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2017-03-23 15:31
 */
@RestController
@RequestMapping("/api")
@Api("用户接口")
public class ApiUserController {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysUserTokenService sysUserTokenService;
    @Autowired
    private MerchantService merchantService;

    /**
     * 登录
     */
    @AuthIgnore
    @PostMapping("login")
    @ApiOperation(value = "登录", notes = "登录说明")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "mobile", value = "手机号", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "password", value = "密码", required = true)
    })
    public ReturnResult login(String mobile, String password, HttpServletRequest request) {
        Assert.isBlank(mobile, "手机号不能为空");
        Assert.isBlank(password, "密码不能为空");

        String clientIP = IPUtils.getIpAddr(request);//客户端ip
        //用户登录
        SysUserEntity loginUser = sysUserService.login(mobile, password);
        Merchant merchant = merchantService.queryForUserId(loginUser.getUserId());

        //生成token
        Map<String, Object> map = sysUserTokenService.createToken(loginUser.getUserId());
        map.put("merchantId", merchant.getId());
        String storeIds = loginUser.getStoreIds();
        if (StringUtils.isEmpty(storeIds)) {
            map.put("storeId", 0);
        } else {
            map.put("storeId", loginUser.getCurrentStoreId());
        }
        return ReturnResult.ok().put(Constants.DEFAULT_RESULT_NAME,map);
    }

    /**
     * 修改密码
     */
    @SysLog("修改密码")
    @PostMapping("updatep")
    @ApiOperation(value = "修改密码", notes = "修改密码")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "oPassword", value = "旧密码", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "nPassword", value = "新密码", required = true)
    })
    public ReturnResult updatep(@LoginUser SysUserEntity user, String oPassword, String nPassword) {
        Assert.isBlank(oPassword, "旧密码不能为空!");
        Assert.isBlank(nPassword, "新密码不能为空!");
        sysUserService.updatep(user, oPassword, nPassword);
        return ReturnResult.ok();
    }

    /**
     * 用户信息
     */
    @GetMapping("userInfo")
    @ApiOperation(value = "用户信息", notes = "用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true)
    })
    public ReturnResult userInfo(@LoginUser SysUserEntity user) {
        Map<String, Object> map = new HashedMap();
        map.put("userName", user.getUsername());
        map.put("name", user.getName());
        return ReturnResult.ok().put(Constants.DEFAULT_RESULT_NAME,map);
    }

    /**
     * 退出登陆
     */
    @SysLog("退出登录")
    @GetMapping("logout")
    @ApiOperation(value = "退出登陆", notes = "退出登陆")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true)
    })
    public ReturnResult logout(@LoginUser SysUserEntity user) {
        sysUserTokenService.deleteToken(user.getUserId());
        return ReturnResult.ok();
    }


//    /**
//     * 管理员列表
//     */
//    @GetMapping("manager")
//    @ApiOperation(value = "管理员列表", notes = "管理员列表")
//    @ApiImplicitParams({
//            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true)
//    })
//    public ReturnResult manager(@LoginUser SysUserEntity user) {
//        return sysUserService.managerList(user);
//    }
//
//    /**
//     * 新建管理员
//     */
//    @SysLog("新建管理员")
//    @PostMapping("createM")
//    @ApiOperation(value = "新建管理员")
//    @ApiImplicitParams({
//            @ApiImplicitParam(paramType = "query", dataType = "string", name = "mobile", value = "手机号", required = true),
//            @ApiImplicitParam(paramType = "query", dataType = "string", name = "name", value = "昵称", required = true),
//            @ApiImplicitParam(paramType = "query", dataType = "string", name = "password", value = "密码", required = true),
//            @ApiImplicitParam(paramType = "query", dataType = "string", name = "identifyCode", value = "验证码", required = true),
//            @ApiImplicitParam(paramType = "query", dataType = "string", name = "auth", value = "账号权限  4:普通管理员/ 3:高级管理员", required = true),
//            @ApiImplicitParam(paramType = "query", dataType = "string", name = "merchantCode", value = "商家验证码"),
//            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true)
//    })
//    public ReturnResult register(String mobile, String name, String password, String identifyCode, String merchantCode,
//                                 String auth, @LoginUser SysUserEntity user) {
//        Assert.isBlank(mobile, "手机号不能为空");
//        Assert.isBlank(name, "昵称不能为空");
//        Assert.isBlank(password, "密码不能为空");
//        Assert.isBlank(identifyCode, "验证码不能为空");
//        Assert.isBlank(auth, "权限类型不能为空");
//        if (String.valueOf(Constants.ROLE_ID_STOREADMIN).equals(auth)) {
//            Assert.isBlank(merchantCode, "商家验证码");
//        }
//        sysUserService.saveManager(mobile, password, name, identifyCode, merchantCode, auth, user);
//        return ReturnResult.ok();
//    }
//
//    /**
//     * 解绑管理员
//     */
//    @SysLog("解绑管理员")
//    @PostMapping("freeManager")
//    @ApiOperation(value = "解绑管理员", notes = "解绑管理员")
//    @ApiImplicitParams({
//            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true),
//            @ApiImplicitParam(paramType = "query", dataType = "string", name = "userId", value = "用户id", required = true)
//    })
//    public ReturnResult manager(@LoginUser SysUserEntity user,String userId) {
//        Assert.isBlank(userId,"用户id不能为空!");
//        return sysUserService.freeManager(user,userId);
//    }

}
